package com.wn.icloud.provider.uac.rpc;

import com.wn.icloud.provider.uac.entity.Group;
import com.wn.icloud.provider.uac.mapper.GroupMapper;
import com.wn.icloud.provider.uac.remote.GroupFeignApi;
import com.wn.icloud.provider.uac.remote.vo.GroupOrgInfResVo;
import com.wn.icloud.provider.uac.service.GroupBiz;
import com.wn.icloud.provider.uac.service.GroupTypeService;
import com.wn.icloud.rest.BaseController;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

@RequestMapping("/")
@RestController
public class GroupRest extends BaseController<GroupBiz, Group> implements GroupFeignApi {

    @Autowired
    private GroupTypeService groupTypeService;

    @Autowired
    private GroupMapper groupMapper;

    /**
     * 获取组织车间数据
     * @param
     * @return List<GroupOrgInfResVo>
     */
    @RequestMapping(value = "/group/groupMcOrgInfo", method = RequestMethod.GET)
    @ResponseBody
    @Override
    public List<GroupOrgInfResVo> groupMcOrgInfo(){
        Integer groupType = groupTypeService.getOrgInfoType();
        Example example = new Example(Group.class);
        example.createCriteria().andNotEqualTo("parentId", -1);
        example.createCriteria().andEqualTo("groupType", groupType);
        List<Group> groups = baseBiz.selectByExample(example);
        List<GroupOrgInfResVo> response = new ArrayList<>();
        for(Group group : groups){
            if(groupType == group.getGroupType()){
                if(-1 == group.getParentId()){
                    continue;
                } else{
                    GroupOrgInfResVo vo = new GroupOrgInfResVo();
                    vo.setValue(group.getCode());
                    vo.setLabel(group.getName());
                    response.add(vo);
                }
            }
        }
        return response;
    }

    /**
     * 根据车间组织编号获取车间组织信息
     * @param orgCode
     * @return GroupOrgInfResVo
     */
    @RequestMapping(value = "/group/getGroupOrgInfoByOrgCode", method = RequestMethod.GET)
    @ResponseBody
    @Override
    public GroupOrgInfResVo getGroupOrgInfoByOrgCode(String orgCode) {
        GroupOrgInfResVo response = new GroupOrgInfResVo();
        Example example = new Example(Group.class);
        example.createCriteria().andNotEqualTo("code", orgCode);
        List<Group> groups = baseBiz.selectByExample(example);
        for(Group group : groups){
            response.setValue(group.getId().toString());
            response.setLabel(group.getName());
        }
        return response;
    }

    /**
     * 根据车间组织名称获取车间组织id
     * @param orgName
     * @return Integer
     */
    @RequestMapping(value = "/group/getIdByName", method = RequestMethod.GET)
    @ResponseBody
    @Override
    public Integer getIdByName(@RequestParam("orgName") String orgName){
        Integer id = null;
        Example example = new Example(Group.class);
        example.createCriteria().andEqualTo("name", orgName);
        List<Group> groups = baseBiz.selectByExample(example);
        for(Group group : groups){
            id = group.getId();
        }
        return id;
    }

    /**
     * 根据车间组织Id获取车间组织信息
     * @param orgId
     * @return GroupOrgInfResVo
     */
    @RequestMapping(value = "/group/getGroupOrgInfoByOrgId", method = RequestMethod.GET)
    @ResponseBody
    @Override
    public GroupOrgInfResVo getGroupOrgInfoByOrgId(Integer orgId) {
        GroupOrgInfResVo response = new GroupOrgInfResVo();
        Group group = baseBiz.selectById(orgId);
        response.setValue(group.getId().toString());
        response.setLabel(group.getName());
        return response;
    }

    /**
     * 根据用户id获取所在车间组织信息
     * @param userId
     * @return GroupOrgInfResVo
     */
    @RequestMapping(value = "/group/getGroupOrgByUserId", method = RequestMethod.GET)
    @ResponseBody
    @Override
    public GroupOrgInfResVo getGroupOrgByUserId(@RequestParam("userId") String userId) {
        // 根据用户id查询车间组织角色id
        System.out.println("userId===" + userId);
        String orgId = groupMapper.selectGroupLeadersByUserId(userId);
        System.out.println("orgId==============" + orgId);
        GroupOrgInfResVo response = null;
        if(StringUtils.isNotBlank(orgId)){
            response = new GroupOrgInfResVo();
            // 根据车间组织角色id查询车间组织信息
            Group group = baseBiz.selectById(Integer.parseInt(orgId));
            response.setValue(group.getId().toString());
            response.setLabel(group.getName());
        }
        return response;
    }
}
